package com.example.arithmeticleetcode.leetcode.jan;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2021-01-21 09:34
 **/
public class Demo17 {

    public static void main(String[] args) {
        int[] nums = {4,6,5,9,3,7};
        int[] arr = new int[3];
        System.arraycopy(nums, 0, arr, 0, arr.length);
        System.out.println(Arrays.toString(arr));
    }

    public List<Boolean> checkArithmeticSubarrays(int[] nums, int[] l, int[] r) {
        List<Boolean> list = new ArrayList<>();
        int index = 0;
        while (index < l.length) {
            int[] arr = new int[r[index] - l[index] + 1];
            System.arraycopy(nums, l[index], arr, 0, arr.length);
            index++;
            list.add(isDiffArr(arr));
        }
        return list;
    }

    private boolean isDiffArr(int[] arr) {
        if (arr.length < 3) return true;
        Arrays.sort(arr);
        int diff = arr[1] - arr[0];

        for (int i = 2, len = arr.length; i < len; i++) {
            if (diff != arr[i] - arr[i - 1]) {
               return false;
            }
        }
        return true;
    }
}
